package com.publicize.controller;


import com.publicize.commom.commonutils.JwtUtils;
import com.publicize.commom.commonutils.Result;
import com.publicize.entity.Manage;
import com.publicize.entity.Vo.ManageLogin;
import com.publicize.service.ManageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 管理员登录表 前端控制器
 * </p>
 *
 * @author bs
 * @since 2021-03-02
 */
@RestController
@RequestMapping("/backstage/manage")
@CrossOrigin
public class ManageController {
    @Resource
    private ManageService manageService;
//管理员登录
    @PostMapping("manageLogin")
    public Result manageLogin(@RequestBody ManageLogin manageLogin){
         String token=manageService.login(manageLogin);//登录验证并返回token
        return Result.ok().data("token",token);
    }
    //退出登录
@PostMapping("quitManage")
public Result quitManage(HttpServletRequest httpServletRequest){
    boolean removeToken = JwtUtils.removeToken(httpServletRequest);
    return removeToken==true?Result.ok():Result.error();
}
 //添加管理员
   @PostMapping("addManage")
   public Result addManage(@RequestBody Manage manage){
        manageService.addManage(manage);
        return Result.ok();
   }

    //编辑管理员
    @PostMapping("updateManage")
    public Result updateManage(@RequestBody Manage manage){
        manageService.updateManage(manage);
        return Result.ok();
    }
    //根据条件查询所有管理员
    /**
     *
     * @param page 页数
     * @param limit 每页数据
     * @Param manageUsername 管理员用户名
     * @return
     */
    @GetMapping("selectManage/{page}/{limit}")
    public Result selectManage(@PathVariable("page") Integer page,
    @PathVariable("limit") Integer limit,
    @RequestParam(required = false) String condition
    ){
        return manageService.selectManage(page,limit,condition);
    }
    //根据id删除管理员
    @PostMapping("deleteManage/{id}")
    public Result deleteManage(@PathVariable("id") String id){
        manageService.removeManage(id);
        return Result.ok();
    }
    //根据id将管理员添加至黑名单
    @PostMapping("BlacklistManage/{id}")
    public Result BlacklistManage(@PathVariable("id") String id){
        Integer is_balck=manageService.BlacklistManage(id);
        return Result.ok().data("is_balck",is_balck);
    }
    //根据id查询管理员
    @PostMapping("selectManageById")
    public Result selectManageById(HttpServletRequest request){
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        Manage manage = manageService.getById(memberIdByJwtToken);
        return Result.ok().data("manage",manage);
    }

    //根据id查询管理员
    @GetMapping("getManage")
    public Result getManageInfo(HttpServletRequest request){
        System.out.println(request.getHeader("token"));
        String id = JwtUtils.getMemberIdByJwtToken(request);
        System.out.println("=====================================================");
        System.out.println(id);
        Manage manage = manageService.getById(id);
        System.out.println(manage);
        return Result.ok().data("manage",manage);
    }

    @PostMapping("getManageUpdateInfo/{id}")
    public Result getManageUpdateInfo(@PathVariable String id){
        Manage manage = manageService.getById(id);
        return Result.ok().data("manage",manage);
    }
}

